git cookbook

If you follow along with actual files of your own, it will slow you down, but the hands on experience will be worth it.

We are going to learn about :

init, restore, add, commit, log, diff, revert, checkout, stash, merge, branch, push, pull

 

 

    consolas

    Solo developing, multiple features in parallel

    branches

     

    after stage "repeat indefinitely" from previous explanation

    1. Create a new branch
      >git checkout -b <branch-name>
    2. Now it is possible to make changes to files in multiple branches.
      1. However, moving between branches when new text is written and staged will prompt this error message:
        error: Your local changes to the following files would be overwritten by checkout:
                <list-of-changed-files>
        Please commit your changes or stash them before you switch branches.
        Aborting
      2. If the change in current branch is not staged then we will get this error message:
        error: The following untracked working tree files would be overwritten by checkout:
                <list-of-changed-files>
        Please move or remove them before you switch branches.
        Aborting

    our options are to (add>)commit or (add>)stash. in case we don't want to commit, we stash
    >git stash
    pay attention: any change to files that is not committed or stashed will follow you to other branches

    1. now it is actually possible to move away from the new branch with the change.
      git checkout <branch-name>
    2. when going back to the branch with stashed changes, in order to unstash the changed we run
      >git stash pop
      if we want to just see the stashed changed:
      >git stash list
      >git stash show
    3. So we know how to work in multiple branches, now let's merge them.
      >
      git checkout <branch-we-want-to-merge-into>
      >git merge <branch-we-want-to-merge-from>
    4. If we no longer need the branch we merged from, we delete it
      >
      git branch -d <branch-we-merged-from>

     

     

 

    consolas

    My own explanations

     

    solo developing, 1 feature at a time

    No branches, no pushing and pulling, only adds and commits and rolling back unwanted changes in every step of the way.

     

    1. create a project (at the bare minimum - a directory).
    2. create git reopsitory in the root of the project.
      >cd <path-to-prj>
      >git init .
    3. create files
      >
      touch <files>

    git is aware of these files but they are untracked.

    1. we can make changes to these untracked files. In a way they are tracked because it's possible to revert the changes made in them since last commit

    rolling back untracked changes

    1. >git restore <file-path>
    1. track the files
      >git add <files>
      now they are "changes to be committed" meaning they are
      staged
    2. make modifications to the files
      >echo "<text>" >! <files>
      these modifications are
      not staged. at this point a git commit will not include the modification.
    3. stage the modified files
      >git add <files>
    4. commit the modifications
      >git commit
    5. repeat 5-8 indefinitely until the project is finished.

    rolling back a commit or commits

    1. see commit history
      >
      git log
    2. go over commits, check differences and find the commit you want to go back to
      >
      git diff <commit-hash>~ <commit-hash> - shows commit diff with ancestor
      >
      git diff <commit-hash> - shows commit diff with head
      (google the difference between git diff and git difftool)
       

    revert in current branch

    1. >git revert HEAD~<num-commits-to-revert>..HEAD

    revert in new branch

    1. Create a new branch from this commit - the new branch will have the old code
      >
      git checkout <commit hash> -b <new-branch-name>

 

 

 

    consolas

    Upload project to github and make contributions to it (no collaboration).

     

    1. Init a git repo in the directory of the project
      >cd <project root path>

    >git init .

    1. create remote repo through github.com or gh cli
    2. synchronize remote and local repos
      1. if repo was created through the website
        >
        git clone <repo url> .

    If you already have files there, git will complain that the directory is not empty. Easiest solution I found was to move them, clone, and bring them back, unfortunately.

    Or

    >gh repo clone <user>/<repo>

    1. if repo was created locally through gh CLI

    >git push

    1. Now just write your code, commit often, and push reponsibly.
    2. If you are doing part of the changes from github.com, you will need to download the changes
      >
      git pull

     

     

    View diff between local and remote (to track your changes)

     

    1. >git difftool <branch-name> origin/<branch-name>

 

consolas

Collaboration (not including fork)

 

Take all you learned from the other recipes and just add

>git pull from time to time to get the code other people wrote.

 

This section is under-researched so I wouldn't listen to me.

There are probably conventions on merging branches to main which I'm not familiar enough with yet.

 

Contribute to a repository (fork)

 

Need to write this recipe

 

https://docs.github.com/en/get-started/quickstart/fork-a-repo

 

 

 

 

 

 

 

 

Created with OneNote.